<template>
  <div class="app-container">
    <!-- v-permission="['eqt:eqtClass:read']"
    v-permission="['eqt:eqtClass:create']"
    v-permission="['eqt:eqtClass:update']"
    v-permission="['eqt:eqtClass:delete']" -->
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model.trim="listQuery.fclassstname" clearable class="filter-item" style="width: 200px;" placeholder="请输入分类名称"/>
      <el-button class="filter-item" :style="{background:buttonColor.lookup,color:buttonColor.color}" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button class="filter-item" :style="{background:buttonColor.add ,color:buttonColor.color}" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column :index="indexMethod" align="center" label="序" type="index"/>
      <el-table-column align="center" label="分类编号" prop="fclassstno"/>
      <el-table-column align="center" label="分类名称" prop="fclassstname"/>
      <el-table-column align="center" label="上级分类" prop="fparentno"/>
      <el-table-column align="center" label="分类类别" prop="fclassstyle"/>
      <el-table-column align="center" label="分类级别" prop="flevel"/>
      <el-table-column align="center" label="折扣年限" prop="fdiscmonth"/>
      <el-table-column :formatter="dateFormat" align="center" label="修改时间" prop="fupddate"/>
      <!--<el-table-column align="center" label="操作员" prop="foperator"/>
-->
      <el-table-column align="center" width="140" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <el-button :style="{background:buttonColor.edit,color:buttonColor.color}" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button :style="{background:buttonColor.delete,color:buttonColor.color}" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="分类编号" prop="fclassstno">
          <el-input v-model="dataForm.fclassstno" :disabled="dialogStatus=='update'"/>
        </el-form-item>
        <el-form-item label="分类名称" prop="fclassstname">
          <el-input v-model="dataForm.fclassstname"/>
        </el-form-item>
        <el-form-item label="上级分类" prop="fparentno">
          <el-input v-model="dataForm.fparentno"/>
        </el-form-item>
        <el-form-item label="分类类别" prop="fclassstyle">
          <el-input v-model="dataForm.fclassstyle"/>
        </el-form-item>
        <el-form-item label="分类级别" prop="flevel">
          <el-input v-model="dataForm.flevel"/>
        </el-form-item>
        <el-form-item label="折扣年限" prop="fdiscmonth">
          <el-input v-model="dataForm.fdiscmonth"/>
        </el-form-item>
        <!--<el-form-item label="操作员" prop="foperator">
          <el-input v-model="dataForm.foperator"/>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="createData">确定</el-button>
        <el-button v-else :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="updateData">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listEqtClass, createEqtClass, updateEqtClass, deleteEqtClass } from '@/api/eqtClass'
import Pagination from '@/components/Pagination'
import moment from 'moment/moment'
import {buttonColor} from '@/filters/index'
export default {
  name: 'EqtClass',
  components: { Pagination },
  data() {
    return {
      buttonColor:buttonColor,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fclassstname: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      dataForm: {
        id: undefined,
        fclassstno: undefined,
        fclassstname: undefined,
        fparentno: undefined,
        fclassstyle: undefined,
        flevel: undefined,
        fdiscmonth: undefined,
        fsiteState: undefined,
        fupddate: undefined,
        foperator: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        fclassstno: [
          { required: true, message: '分类编号不能为空', trigger: 'blur' }
        ],
        fclassstname: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ],
        fparentno: [
          { required: true, message: '上级分类不能为空', trigger: 'blur' }
        ],
        fclassstyle: [
          { required: true, message: '分类类别不能为空', trigger: 'blur' }
        ],
        flevel: [
          { required: true, message: '分类级别不能为空', trigger: 'blur' }
        ],
        fdiscmonth: [
          { required: true, message: '折扣年限不能为空', trigger: 'blur' }
        ]
      },
      permissionDialogFormVisible: false,
      systemPermissions: null,
      assignedPermissions: null,
      permissionForm: {
        roleId: undefined,
        permIds: []
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    formatStatus: function(row, column) {
      return row.fsiteState === 1 ? '启用' : '禁用'
    },
    dateFormat(row, column) {
      const date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    getList() {
      this.listLoading = true
      listEqtClass(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        fclassstno: undefined,
        fclassstname: undefined,
        fparentno: undefined,
        fclassstyle: undefined,
        flevel: undefined,
        fdiscmonth: undefined,
        fsiteState: undefined,
        fupddate: undefined,
        foperator: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createEqtClass(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加设备分类成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.msg
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updateEqtClass(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.fclassstno === this.dataForm.fclassstno) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新管理员成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.msg
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除该设备分类资料', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteEqtClass(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除设备分类资料成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.msg
            })
          })
      })
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
